part of '../../repository.dart';

class _YookassaPurchasedSubscriptionTransactionDto extends Equatable
    implements _WebPurchasedSubscriptionTransactionDto, _YookassaTransactionDto {
  const _YookassaPurchasedSubscriptionTransactionDto._internal({
    required this.id,
    required this.originalTransactionId,
    required this.userId,
    required this.purchaseItemSku,
    required this.amountDto,
    required this.offerTypeDto,
    required this.paymentMethodId,
    required this.timeDto,
    required this.test,
    required this.yookassaPaymentId,
    required this.revocationInfoDto,
    required this.incomeAmountDto,
  });

  @override
  final String id;

  @override
  final String originalTransactionId;

  @override
  final String userId;

  @override
  final String purchaseItemSku;

  @override
  final _MoneyAmountDto amountDto;

  @override
  final _WebPurchasedSubscriptionTransactionOfferTypeDto offerTypeDto;

  @override
  final String paymentMethodId;

  @override
  final _TimeDto timeDto;

  @override
  final bool test;

  @override
  final String yookassaPaymentId;

  @override
  final _YookassaTransactionRevocationInfoDto? revocationInfoDto;

  @override
  final _MoneyAmountDto incomeAmountDto;

  @override
  String get paymentSystem => _kWebTransactionYookassaPaymentSystemValue;

  @override
  String get type => _kWebPurchasedSubscriptionTransactionTypeValue;

  @override
  List<Object?> get props {
    return [
      id,
      originalTransactionId,
      userId,
      purchaseItemSku,
      amountDto,
      offerTypeDto,
      paymentMethodId,
      timeDto,
      yookassaPaymentId,
      revocationInfoDto,
      incomeAmountDto,
    ];
  }

  factory _YookassaPurchasedSubscriptionTransactionDto.fromJson(Map<String, dynamic> json) {
    final amountJson = json[_kWebTransactionAmountKey] as Map<String, dynamic>;
    final amountDto = _MoneyAmountDto.fromJson(amountJson);

    final offerTypeString = json[_kWebPurchasedSubscriptionTransactionOfferTypeKey] as String;
    final offerTypeDto =
        _WebPurchasedSubscriptionTransactionOfferTypeDto.fromString(offerTypeString);

    final transactionTimeString = json[_kWebTransactionTimeKey] as String;
    final transactionTimeDto = _TimeDto.fromString(transactionTimeString);

    final revocationInfoJson =
        json[_kYookassaTransactionRevocationInfoKey] as Map<String, dynamic>?;

    _YookassaTransactionRevocationInfoDto? revocationInfoDto;

    if (revocationInfoJson != null) {
      revocationInfoDto = _YookassaTransactionRevocationInfoDto.fromJson(revocationInfoJson);
    }

    final incomeAmountJson = json[_kYookassaTransactionIncomeAmountKey];
    final incomeAmountDto = _MoneyAmountDto.fromJson(incomeAmountJson);

    return _YookassaPurchasedSubscriptionTransactionDto._internal(
      id: json[_kWebTransactionIdKey],
      userId: json[_kWebTransactionUserIdKey],
      purchaseItemSku: json[_kWebTransactionPurchaseItemSkuKey],
      amountDto: amountDto,
      offerTypeDto: offerTypeDto,
      paymentMethodId: json[_kWebTransactionPaymentMethodIdKey],
      timeDto: transactionTimeDto,
      test: json[_kWebTransactionTestKey],
      yookassaPaymentId: json[_kYookassaTransactionYookassaPaymentIdKey],
      revocationInfoDto: revocationInfoDto,
      incomeAmountDto: incomeAmountDto,
      originalTransactionId: json[_kWebPurchasedSubscriptionTransactionOriginalTransactionIdKey],
    );
  }

  factory _YookassaPurchasedSubscriptionTransactionDto.fromEntity(
      YookassaPurchasedSubscriptionTransaction transaction) {
    final amountDto = _MoneyAmountDto.fromEntity(transaction.amount);
    final offerTypeDto =
        _WebPurchasedSubscriptionTransactionOfferTypeDto.fromEntity(transaction.offerType);
    final transactionTimeDto = _TimeDto.fromEntity(transaction.time);
    final incomeAmountDto = _MoneyAmountDto.fromEntity(transaction.incomeAmount);

    _YookassaTransactionRevocationInfoDto? revocationInfoDto;

    if (transaction.revocationInfo != null) {
      revocationInfoDto =
          _YookassaTransactionRevocationInfoDto.fromEntity(transaction.revocationInfo!);
    }

    return _YookassaPurchasedSubscriptionTransactionDto._internal(
      id: transaction.id,
      userId: transaction.userId,
      purchaseItemSku: transaction.purchaseItemSku,
      amountDto: amountDto,
      offerTypeDto: offerTypeDto,
      paymentMethodId: transaction.paymentMethodId,
      timeDto: transactionTimeDto,
      test: transaction.test,
      yookassaPaymentId: transaction.yookassaPaymentId,
      revocationInfoDto: revocationInfoDto,
      incomeAmountDto: incomeAmountDto,
      originalTransactionId: transaction.originalTransactionId,
    );
  }

  @override
  Map<String, dynamic> toJson() {
    return {
      _kWebTransactionPaymentSystemKey: paymentSystem,
      _kWebTransactionTypeKey: type,
      _kWebTransactionIdKey: id,
      _kWebPurchasedSubscriptionTransactionOriginalTransactionIdKey: originalTransactionId,
      _kWebTransactionUserIdKey: userId,
      _kWebTransactionPurchaseItemSkuKey: purchaseItemSku,
      _kWebTransactionPaymentMethodIdKey: paymentMethodId,
      _kWebTransactionAmountKey: amountDto.toJson(),
      _kWebPurchasedSubscriptionTransactionOfferTypeKey: offerTypeDto.toString(),
      _kWebTransactionTimeKey: timeDto.toString(),
      _kWebTransactionTestKey: test,
      _kYookassaTransactionYookassaPaymentIdKey: yookassaPaymentId,
      _kYookassaTransactionRevocationInfoKey: revocationInfoDto?.toJson(),
      _kYookassaTransactionIncomeAmountKey: incomeAmountDto.toJson(),
    };
  }

  @override
  YookassaPurchasedSubscriptionTransaction toEntity() {
    return YookassaPurchasedSubscriptionTransaction(
      id: id,
      originalTransactionId: originalTransactionId,
      userId: userId,
      purchaseItemSku: purchaseItemSku,
      paymentMethodId: paymentMethodId,
      amount: amountDto.toEntity(),
      offerType: offerTypeDto.toEntity(),
      time: timeDto.toEntity(),
      test: test,
      yookassaPaymentId: yookassaPaymentId,
      revocationInfo: revocationInfoDto?.toEntity(),
      incomeAmount: incomeAmountDto.toEntity(),
    );
  }
}
